package br.com.admin.datatable;

import java.util.List;

import br.com.commons.utility.datatable.builder.sql.DataTableSQLBuilder;

@SuppressWarnings("unchecked")
public class DataSourceCountSQLBuilder extends DataTableSQLBuilder {
	
	private void addWhereClause(String column, String searchValue) {
		addWhere(new StringBuilder("obj.").append(column).append(" like '%").append(searchValue).append("%'").toString());
	}

	@Override
	protected void builderColumns() {
		addColumns(" count(obj.id) ");
	}

	@Override
	protected void builderFrom() {
		addFrom(" DataSource obj ");		
	}

	@Override
	protected void builderWhere() {
		if(String.class.cast(attributes[1]) != ""){
			List<String> columns = (List<String>) attributes[0];
			for (int i = 0; i < columns.size(); i++) {
				if(i > 0){ addWhere(" OR "); }
				addWhereClause(columns.get(i), String.class.cast(attributes[1]));			
			}
		}	
	}

	@Override
	protected void builderOrderBy() {}

	@Override
	protected void builderExtra() {}
}
